package com.orange.controller;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.handler.AuthTestFileSchoolService;
import com.orange.modules.entity.AuthTestFileSchoolMap;
import com.orange.util.ReturnCode;


@Controller
@RequestMapping("/testFileSchool")
public class AuthTestFileSchoolController extends BaseController{

	private static final Logger logger = Logger.getLogger(AuthTestFileSchoolController.class);
	
	@Autowired
	private AuthTestFileSchoolService authTestFileSchoolService;
	
	/**
	 * 新增组长抽取的测试题
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/insertTestFileSchool" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> insertTestFileSchool(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			String sid = map.get("sid");
			String fid = map.get("fid");
			String cid = map.get("cid");
			if(sid==null || StringUtils.isBlank(sid))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"学校ID不能为空");
			if(fid==null || StringUtils.isBlank(fid))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"试题ID不能为空");
			if(cid==null || StringUtils.isBlank(cid))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"目录ID不能为空");
			AuthTestFileSchoolMap at = new AuthTestFileSchoolMap();
			at.setCid(cid);
			at.setCreatetime(sdf.format(new Date()));
			at.setFid(fid);
			at.setSid(sid);
			authTestFileSchoolService.insertAuthTestFileSchool(at);
			return toResponse(at,ReturnCode.SUCCESS,"新增成功");
		} catch (Exception e) {
			// TODO: handle exception
			return toResponse(null,ReturnCode.FAIL,"新增失败");
		}
		
	}
	
}
